from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import redirect
from repoistory import models
from django.http import Http404
from backend.libs import forms
from django.urls import reverse
import json

# Create your views here.

def index(request):
    '''后台管理首页'''
    return render(request, 'backend/index.html')


def project_detail(request):
    return render(request, 'backend/project_detail.html')

def add_project(request):
    return render(request, 'backend/add.html')


def delete(request,model_name):
    if request.method == 'POST':
        res_dict = {'status': True, 'error': None, 'data': None}
        res_dict['data'] = 'ok'
        if not hasattr(models,model_name):
            raise Http404()
        model = getattr(models,model_name)
        nid = request.POST.get('nid')
        if model.objects.filter(id=nid).exists():
            model.objects.filter(id=nid).delete()
            res_dict['data'] = 'ok'
        else:
            res_dict['status'] = False

        return HttpResponse(json.dumps(res_dict))